<?php
    
    
    include_once "include/constants.php";
	
    if (!defined('IS_AJAX')) {
        define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
    }

	if (!isset($_GET["userid"])) {
        die("Give me params!!!");
    }

    try {
        $conn = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USER, DB_PASS);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
        $thumbs_UserId = $_GET["userid"];
         
        $stmt = $conn->prepare('select * from movies where movieid not in
                                (select movieid from user_ratings where userid = :userid)
                                order by rand() limit 3;');
		$stmt->execute(array("userid" => $thumbs_UserId));
        
        $taste_data = array();
		$forbidden = "";
        while ($taste_row = $stmt->fetch()) {
            $taste_data[] = $taste_row;
			$forbidden .= $taste_row['movieid'] . ",";
        }
		$forbidden = substr_replace($forbidden, "", -1);
		
		$stmt = $conn->prepare('select * from (select m.movieid,m.title,m.year,m.imdbid from movies m, ratings r
								where m.movieid = r.movieid
								and votes > 50000
								and  m.movieid not in
								(select movieid from user_ratings where userid = 7)
								and m.movieid not in
								('.$forbidden.')
								order by rank desc limit 100) t
								order by rand() limit 3;');
		$stmt->execute(array("userid" => $thumbs_UserId));
        
        while ($taste_row = $stmt->fetch()) {
            $taste_data[] = $taste_row;
        }
		
		shuffle($taste_data);
        
        echo json_encode($taste_data);
        
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    
?>